package com.k2data.k2app.service.pas04;


import com.k2data.k2app.common.K2Assert;
import com.k2data.k2app.common.ResponseConstant;
import com.k2data.k2app.domain.po.JobTaskDO;
import com.k2data.k2app.domain.vo.Job;
import com.k2data.k2app.exception.K2ResponseException;
import com.k2data.k2app.mapper.TaskMapper;
import com.k2data.k2app.service.JobService;
import com.k2data.k2app.service.JobTaskService;
import com.k2data.k2app.util.JsonUtils;
import com.k2data.k2app.utils.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从pas取回来的数据
 * Created by clb on 17-3-30.
 */
@Log4j2
@Service
public class Pas04ResultService extends Pas04Basic {
	@Autowired
	private JobService jobService;
	@Autowired
	private JobTaskService jobTaskService;

	//job result
	public List results(Long jobId,String type) throws Exception{
		List result = new ArrayList();
		if("file".equals(type.toLowerCase())){
			result = getHdfsFiles(jobId);
		}else if("interface".equals(type.toLowerCase())){
			result = getInterfaces(null,jobId,null,null,null);
		}
		return result;
	}

	/**
	 * <pre>
	 * 查询接口数据
	 * @param jobId 作业id
	 * @return
	 * </pre>
	 */
	public List getInterfaces(Long projectId,Long jobId,String deviceNo,Integer pageNum,Integer pageSize)  throws Exception{
		Map<String,Object> map = new HashMap<>();
		if(projectId == null){
			if(jobId != null){
				JobTaskDO jobTaskDO = jobTaskService.getByJobId(jobId);
				if(jobTaskDO != null){
					projectId = jobTaskDO.getProjectId();
				}else{
					throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
							String.format("%s(id: %s)", ResponseConstant.MSG_JOB_ID_NOT_FOUND, jobId));
				}
			}
		}
		map.put("projectId",projectId);
		if(jobId != null){
			map.put("instanceId",jobId);
		}
		if(deviceNo != null){
			map.put("condition","deviceno='"+deviceNo+"'");
		}
		if(pageNum != null ){
			map.put("num",pageNum);
		}
		if(pageSize != null ){
			map.put("size",pageSize);
		}
		String condition = getQueryCondition(map);
		String infoError = getFromPas(getResultUrl()+"?"+condition);
		Map resultMap = JsonUtils.convertJsonStrToMap(infoError);
		if(resultMap.get("body")==null){
			log.debug("接口内容为空!");
			return new ArrayList();
		}
		K2Assert.isExceptionByPas(resultMap);
		Map body = JsonUtils.convertJsonStrToMap(resultMap.get("body").toString());
		if(body.size()==0){
			return new ArrayList();
		}
		List items = JsonUtils.convertJsonStrToList(body.get("items").toString());
		return items;
	}
/*
	public List listResultData(Long devicePart,Long projectId,Long jobId,String deviceNo) throws Exception {
		if(devicePart == null) {
			return getInterfaces(projectId,jobId,deviceNo,null,null);
		}
		List list = new ArrayList();
		//获取所有复制模型
		TaskDTO taskDO = new TaskDTO();
		taskDO.setDeviceParts(String.valueOf(devicePart));
		List<TaskDTO> tasks = taskMapper.queryList(taskDO);
		for(TaskDTO taskDO1 :tasks){
			if(taskDO1.getModelIdCopy() == null){
				continue;
			}
			Long modelId = taskDO1.getModelIdCopy();
			List<Job> jobs =  jobService.listJobs(null,modelId,1,30,null);
			if(jobs.size() == 0){
				continue;
			}
			for(Job job:jobs){
				if(job.getHasApi().equals("false")){
					continue;
				}
				List results = getInterfaces(modelId,jobId,deviceNo,null,null);
				list.addAll(results);
			}
		}
		return list;
	}
*/


	/**
	 * <pre>
	 * 查询文件
	 * 根据hdfs结果路径查询所有结果文件
	 * @param path 	文件路径
	 * @param expand expand=true查询目录下所有文件【包括子目录下文件】，expand=false查询目录下内容【文件和文件夹】
	 * @return
	 * </pre>
	 */
	public List getFilesByPath(String path,boolean expand)  throws Exception{
		Map<String,Object> map = new HashMap<>();
		if(!StringUtils.isEmpty(path)){
			map.put("filePath",path);
			map.put("expand",expand);
		}
		String condition = getQueryCondition(map);
		String infoError = "";
		infoError = getFromPas(getHdfsUrl()+"?"+condition);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
		List items = JsonUtils.convertJsonStrToList(resultMap.get("result").toString());
		return items;
	}

	public String getFileDownLoadUrl(String path,String fileName) throws K2ResponseException {
		String url = getHdfsDownLoadUrl(path,fileName);
		return getURI(url);
	}

	public List getHdfsFiles(Long jobId)  throws Exception{
		Job job = jobService.get(jobId);
		List map = getFilesByPath(job.getHdfsPath(),false);
		return map;
	}

}
